/*
 * Copyright © 2022-now 尛飛俠（Denvie） All rights reserved.
 */

package cn.denvie.springcloud.user;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 用户中心Application。
 *
 * @author Denvie
 * @date 2022/3/29
 * @since 1.0.0
 */
@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
@RefreshScope
public class UserCenterApplication implements ApplicationListener<ApplicationReadyEvent> {
    private AtomicBoolean ready = new AtomicBoolean(false);

    public static void main(String[] args) {
        SpringApplication.run(UserCenterApplication.class, args);
    }

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        // 避免多个 Spring context 导致多次调用
        if (this.ready.compareAndSet(false, true)) {
            log.info("application ready !!!");
            ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
            log.info("author name: {}, author email: {}",
                    environment.getProperty("author.name"), environment.getProperty("author.email"));
        }
    }
}
